package digital.store.springmvc.controller;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import digital.store.springmvc.service.CategoryService;
import digital.store.springmvc.service.OrderService;
import digital.store.springmvc.service.ProductService;
import digital.store.tutorial.springmvc.model.Category;
import digital.store.tutorial.springmvc.model.Order;
import digital.store.tutorial.springmvc.model.Product;
import digital.store.tutorial.springmvc.model.ShoppingCart;


public class ShoppingController {
	
	@Autowired
	private ShoppingCart cart;
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value ="addCart", method = RequestMethod.POST)
	public String addToCart(Product product,HttpSession session)
	  {
		cart.setProduct(product);
		session.setAttribute("listcar", cart);
	     // now just use the cart
		return null;
	  }  
	
	@RequestMapping(value ="checkout", method = RequestMethod.GET)
	public String CheckOut(HttpSession session)
		{
			Order order = new Order();
			order.setProducts(cart.getProducts());
			orderService.saveOrUpdate(order);
			return null;	
		}
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public String showEditForm(@PathVariable Long id, Model model) {
		Collection<Product> products = productService.findByCategoryId(id);
        model.addAttribute("products",products);
        return "user/detail";
    }
}
